package com.x.Servlet_10_三大域对象;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * ClassName: Servlet1
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.08.29 17:10
 */

@WebServlet("/DataTrans1")
public class Servlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //在请求域存放数据
        req.setAttribute("k-request","v-request");

        //在会话域存放数据
        HttpSession session = req.getSession();
        session.setAttribute("k-session","v-session");

        //在应用域存放数据
        ServletContext application = req.getServletContext();
        application.setAttribute("k-app","v-app");

        //请求转发:来测试数据是否传递
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("DataTrans2");
        requestDispatcher.forward(req, resp);
    }
}
